package dontCare.gwtester.client.gwt.leakyAbstraction;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

import dontCare.gwtester.client.tool.SourceUrl;
import dontCare.gwtester.client.tool.UnitWidget;

public class TkcnDate extends UnitWidget {
	private static TkcnDateUiBinder uiBinder = GWT.create(TkcnDateUiBinder.class);
	interface TkcnDateUiBinder extends UiBinder<Widget, TkcnDate>{}
	
	@UiField Label tkcnValue;
	@UiField Label noneNewValue;
	
	public TkcnDate() {
		this.initWidget(uiBinder.createAndBindUi(this));
		Date d1 = new Date();
		Date d2 = new Date();
		tkcnValue.setText(prevMonthStart(d1).toString());
		noneNewValue.setText(prevMonthStartNoneNew(d2).toString());
	}

	@SuppressWarnings("deprecation")
	public static Date prevMonthStart(Date date) {
		date.setDate(0);
		date = new Date(date.getTime());
		date.setDate(1);
		return date;
	}

	@SuppressWarnings("deprecation")
	public static Date prevMonthStartNoneNew(Date date){
		date.setDate(0);
		date.setDate(1);
		return date;
	}
	
	@Override
	public List<String> getSourceUrl() {
		ArrayList<String> result = new ArrayList<String>();
		result.add(SourceUrl.genJava(TkcnDate.class));
		result.add(SourceUrl.genUiXml(TkcnDate.class));
		return result;
	}

	@Override
	public HTML getDescription() {
		return new HTML(
			"用「date.setDate(0); date.setDate(1);」的方式取得上個月 1 號的日期。<br />" +
			"在 Development Mode 時會取得錯誤（當月 1 號）的值，必須用 new Date(date.setDate(0).getTime()).setDate(1) 才能取得正確值。<br/>" +
			"在 Production Mode 則沒有這個問題。"
		);
	}

	@Override
	public String getName() {
		return "tkcn 日期問題";
	}
}